package com.zxx.adapter;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.soufang.ui.R;
import com.zxx.sql.houseData;
/**
 * @brief mhouseList的适配器
 * @author zxx
 * 
 */
public class houseListAdapter extends BaseAdapter {

	private Context context;
	private List<houseData> mlist;
	private int selectedPosition = 0;
	 
	 public houseListAdapter(Context context, List<houseData> list) {
	        this.context = context;
	        this.mlist = list;
	     
	    }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		 return mlist == null ? 0 : mlist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		  ViewHolder holder;
		if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_housedata, null);
            holder = new ViewHolder();
            holder.titleTextView = (TextView)convertView.findViewById(R.id.list_item_title);
            holder.departTextView = (TextView)convertView.findViewById(R.id.list_item_department);
            holder.buildTypeTextView = (TextView)convertView.findViewById(R.id.list_item_buildtype);
            holder.areaTextView = (TextView)convertView.findViewById(R.id.list_item_area);
            holder.imageView=(ImageView)convertView.findViewById(R.id.list_item_imageView);
            holder.totalPriceTextView = (TextView)convertView.findViewById(R.id.list_item_totalPrice);
            convertView.setTag(holder);
		}else{
			
			holder = (ViewHolder) convertView.getTag();
		}
			
			holder.titleTextView.setText(mlist.get(position).getMtitle());
			holder.departTextView.setText(mlist.get(position).getMdepartMentName());
			holder.buildTypeTextView.setText(mlist.get(position).getMbuildType());
			String areaText = mlist.get(position).getMarea()+context.getResources().getString(R.string.pm);
			holder.areaTextView.setText(areaText);
			String priceText = mlist.get(position).getMtotalprice()+context.getResources().getString(R.string.w);
			holder.totalPriceTextView.setText(priceText);
			holder.imageView=(ImageView)convertView.findViewById(R.id.list_item_imageView);
			
			 try {
			      AssetManager assetManager = context.getAssets();
			      String path = mlist.get(position).getMpicPath1();
			      InputStream is = assetManager.open(path);
			      Bitmap bitmap = BitmapFactory.decodeStream(is);
			      if (bitmap != null) {
			    	  holder.imageView.setImageBitmap(bitmap);
			      } else {
			    	  
			      }
			    } catch (Exception e) {
			    	Log.e("houseListAdapter","we got big error here,Exception: "+Log.getStackTraceString(e));
			    }
			    
		return convertView;
	}

	

    public void setSelectedPosition(int selectedPosition) {
        this.selectedPosition = selectedPosition;
    }

    public int getSelectedPosition() {
        return selectedPosition;
    }
    
    private class ViewHolder {
    	TextView titleTextView;
    	TextView departTextView;
    	TextView buildTypeTextView;
    	TextView areaTextView;
    	TextView totalPriceTextView;
    	ImageView imageView;
    }
    
    
    
}
